package com.example.auth.demo.controller;

import com.example.auth.demo.domain.PageResult;
import com.example.auth.demo.domain.ResultCode;
import com.example.auth.demo.domain.ResultJson;
import com.example.auth.demo.domain.auth.*;
import com.example.auth.demo.service.CustomerServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(description = "游戏客服系统相关接口，包含工单、知识库、玩家补偿、封禁、角色及装备锁定等管理")
@RequestMapping("/api/v1/customer-service")
public class CustomerServiceController {

    @Value("${jwt.header}")
    private String tokenHeader;

    private final CustomerServiceService customerServiceService;

    @Autowired
    public CustomerServiceController(CustomerServiceService customerServiceService) {
        this.customerServiceService = customerServiceService;
    }

    // 工单表相关接口
    @PostMapping("/work-order")
    @ApiOperation(value = "创建工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson createWorkOrder(@RequestBody WorkOrder workOrder, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.createWorkOrder(workOrder);
        return ResultJson.ok();
    }

    @GetMapping("/work-order/{id}")
    @ApiOperation(value = "根据工单ID获取工单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<WorkOrder> getWorkOrderById(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        WorkOrder workOrder = customerServiceService.getWorkOrderById(id);
        return ResultJson.ok(workOrder);
    }

    @GetMapping("/work-orders")
    @ApiOperation(value = "分页获取工单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，默认1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数，默认10", dataType = "int", paramType = "query")
    })
    public ResultJson<PageResult<WorkOrder>> getAllWorkOrders(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        PageResult<WorkOrder> pageResult = customerServiceService.getAllWorkOrdersByPage(page, pageSize);
        return ResultJson.ok(pageResult);
    }

    @PutMapping("/work-order")
    @ApiOperation(value = "更新工单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateWorkOrderStatus(@RequestBody WorkOrder workOrder, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.updateWorkOrderStatus(workOrder);
        return ResultJson.ok();
    }
    @DeleteMapping("/work-order/{id}")
    @ApiOperation(value = "根据工单ID删除工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteWorkOrder(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.deleteWorkOrder(id);
        return ResultJson.ok("工单删除成功");
    }

    // 知识库表相关接口
    @PostMapping("/knowledge-base")
    @ApiOperation(value = "创建知识库条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson createKnowledgeBase(@RequestBody KnowledgeBase knowledgeBase, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.createKnowledgeBase(knowledgeBase);
        return ResultJson.ok();
    }

    @GetMapping("/knowledge-base/{id}")
    @ApiOperation(value = "根据知识库ID获取知识库条目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<KnowledgeBase> getKnowledgeBaseById(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        KnowledgeBase knowledgeBase = customerServiceService.getKnowledgeBaseById(id);
        return ResultJson.ok(knowledgeBase);
    }

    @GetMapping("/knowledge-bases")
    @ApiOperation(value = "获取所有知识库条目（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段，默认id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方式，asc/desc，默认desc", dataType = "string", paramType = "query")
    })
    public ResultJson<PageResult<KnowledgeBase>> getAllKnowledgeBases(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "id") String sort,
            @RequestParam(defaultValue = "desc") String order,
            HttpServletRequest request
    ) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        PageResult<KnowledgeBase> pageResult = customerServiceService.getAllKnowledgeBases(page, pageSize, sort, order);
        return ResultJson.ok(pageResult);
    }

    @PutMapping("/knowledge-base")
    @ApiOperation(value = "更新知识库条目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateKnowledgeBase(@RequestBody KnowledgeBase knowledgeBase, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.updateKnowledgeBase(knowledgeBase);
        return ResultJson.ok();
    }

    @DeleteMapping("/knowledge-base/{id}")
    @ApiOperation(value = "删除知识库条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteKnowledgeBase(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.deleteKnowledgeBase(id);
        return ResultJson.ok();
    }

    // 玩家补偿表相关接口
    @PostMapping("/player-compensations")
    @ApiOperation(value = "发放玩家补偿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson createPlayerCompensation(@RequestBody PlayerCompensation playerCompensation, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.createPlayerCompensation(playerCompensation);
        return ResultJson.ok("补偿发放成功");
    }

    @GetMapping("/player-compensations/{playerId}")
    @ApiOperation(value = "根据玩家ID查询补偿记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<PlayerCompensation>> getPlayerCompensationsByPlayerId(@PathVariable Long playerId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<PlayerCompensation> compensations = customerServiceService.getPlayerCompensationsByPlayerId(playerId);
        return ResultJson.ok(compensations);
    }

    @GetMapping("/player-compensations")
    @ApiOperation(value = "查询所有玩家补偿记录（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "playerId", value = "玩家ID筛选", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "itemType", value = "道具类型筛选", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段，默认createTime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方式，asc/desc，默认desc", dataType = "string", paramType = "query")
    })
    public ResultJson<PageResult<PlayerCompensation>> getAllPlayerCompensations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long playerId,
            @RequestParam(required = false) String itemType,
            @RequestParam(defaultValue = "createTime") String sort,
            @RequestParam(defaultValue = "desc") String order,
            HttpServletRequest request
    ) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        PageResult<PlayerCompensation> pageResult = customerServiceService.getAllPlayerCompensations(
                page, pageSize, playerId, itemType, sort, order
        );
        return ResultJson.ok(pageResult);
    }

    @PutMapping("/player-compensations")
    @ApiOperation(value = "更新玩家补偿记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updatePlayerCompensation(@RequestBody PlayerCompensation playerCompensation, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.updatePlayerCompensation(playerCompensation);
        return ResultJson.ok("补偿记录更新成功");
    }

    @DeleteMapping("/player-compensations/{id}")
    @ApiOperation(value = "删除玩家补偿记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deletePlayerCompensation(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.deletePlayerCompensation(id);
        return ResultJson.ok("补偿记录删除成功");
    }

    // 玩家封禁表相关接口
    // PlayerBanController.java
        @PostMapping("/player-bans")
        @ApiOperation(value = "封禁玩家")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
        })
        public ResultJson banPlayer(@RequestBody PlayerBan playerBan, HttpServletRequest request) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            customerServiceService.banPlayer(playerBan);
            return ResultJson.ok("玩家封禁成功");
        }

        @GetMapping("/player-bans/{id}")
        @ApiOperation(value = "根据封禁记录ID查询封禁信息")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
        })
        public ResultJson<PlayerBan> getPlayerBanById(@PathVariable Long id, HttpServletRequest request) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            PlayerBan playerBan = customerServiceService.getPlayerBanById(id);
            return ResultJson.ok(playerBan);
        }

        @GetMapping("/player-bans/player/{playerId}")
        @ApiOperation(value = "根据玩家ID查询封禁记录")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
        })
        public ResultJson<List<PlayerBan>> getPlayerBansByPlayerId(@PathVariable Long playerId, HttpServletRequest request) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            List<PlayerBan> playerBans = customerServiceService.getPlayerBansByPlayerId(playerId);
            return ResultJson.ok(playerBans);
        }

        @GetMapping("/player-bans")
        @ApiOperation(value = "查询所有玩家封禁记录（分页）")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
                @ApiImplicitParam(name = "page", value = "页码，从1开始", required = true, dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "playerId", value = "玩家ID筛选", dataType = "long", paramType = "query"),
                @ApiImplicitParam(name = "startTime", value = "开始时间筛选", dataType = "string", paramType = "query"),
                @ApiImplicitParam(name = "endTime", value = "结束时间筛选", dataType = "string", paramType = "query"),
                @ApiImplicitParam(name = "sort", value = "排序字段，默认startTime", dataType = "string", paramType = "query"),
                @ApiImplicitParam(name = "order", value = "排序方式，asc/desc，默认desc", dataType = "string", paramType = "query")
        })
        public ResultJson<PageResult<PlayerBan>> getAllPlayerBans(
                @RequestParam(defaultValue = "1") int page,
                @RequestParam(defaultValue = "10") int pageSize,
                @RequestParam(required = false) Long playerId,
                @RequestParam(required = false) String startTime,
                @RequestParam(required = false) String endTime,
                @RequestParam(defaultValue = "startTime") String sort,
                @RequestParam(defaultValue = "desc") String order,
                HttpServletRequest request
        ) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            PageResult<PlayerBan> pageResult = customerServiceService.getAllPlayerBans(
                    page, pageSize, playerId, startTime, endTime, sort, order
            );
            return ResultJson.ok(pageResult);
        }

        @PutMapping("/player-bans")
        @ApiOperation(value = "更新玩家封禁记录")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
        })
        public ResultJson updatePlayerBan(@RequestBody PlayerBan playerBan, HttpServletRequest request) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            customerServiceService.updatePlayerBan(playerBan);
            return ResultJson.ok("封禁记录更新成功");
        }

        @DeleteMapping("/player-bans/{id}")
        @ApiOperation(value = "解除玩家封禁")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
        })
        public ResultJson deletePlayerBan(@PathVariable Long id, HttpServletRequest request) {
            String token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
            customerServiceService.deletePlayerBan(id);
            return ResultJson.ok("玩家封禁已解除");
        }

    // 角色及装备锁定表相关接口
    @PostMapping("/role-equipment-lock")
    @ApiOperation(value = "锁定角色或装备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson lockRoleEquipment(@RequestBody RoleEquipmentLock roleEquipmentLock, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        customerServiceService.lockRoleEquipment(roleEquipmentLock);
        return ResultJson.ok();
    }

    @GetMapping("/role-equipment-lock/{id}")
    @ApiOperation(value = "根据锁定记录ID获取角色或装备锁定记录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<RoleEquipmentLock> getRoleEquipmentLockById(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        RoleEquipmentLock roleEquipmentLock = customerServiceService.getRoleEquipmentLockById(id);
        return ResultJson.ok(roleEquipmentLock);
    }

    @GetMapping("/role-equipment-locks/role/{roleId}")
    @ApiOperation(value = "根据角色ID获取角色锁定记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<RoleEquipmentLock>> getRoleEquipmentLocksByRoleId(@PathVariable Long roleId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<RoleEquipmentLock> roleEquipmentLocks = customerServiceService.getRoleEquipmentLocksByRoleId(roleId);
        return ResultJson.ok(roleEquipmentLocks);
    }

    @GetMapping("/role-equipment-locks/equipment/{equipmentId}")
    @ApiOperation(value = "根据装备ID获取装备锁定记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<RoleEquipmentLock>> getRoleEquipmentLocksByEquipmentId(@PathVariable Long equipmentId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<RoleEquipmentLock> roleEquipmentLocks = customerServiceService.getRoleEquipmentLocksByEquipmentId(equipmentId);
        return ResultJson.ok(roleEquipmentLocks);
    }
}